using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;
/// <summary>
/// Summary description for AccountDB
/// </summary>
/// 
namespace CE.User
{
    public class ContactDB
    {
        public ContactDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }



        public Int64 Insert(Contact contact)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocUserContactInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ContactId", contact.ContactId);
            _sPara.Add("@Email", contact.Email == string.Empty ? string.Empty : contact.Email);
            _sPara.Add("@Mobile", contact.Mobile == string.Empty ? string.Empty : contact.Mobile);
            _sPara.Add("@HomePostCode", contact.HomePostCode == string.Empty ? string.Empty : contact.HomePostCode);
            _sPara.Add("@OfficePostCode", contact.OfficePostCode == string.Empty ? string.Empty : contact.OfficePostCode);
            _sPara.Add("@HomeUnitNo", contact.HomeUnitNo);
            _sPara.Add("@OfficeUnitNo", contact.OfficeUnitNo);
            _sPara.Add("@HomeLevelNo", contact.HomeLevelNo);
            _sPara.Add("@OfficeLevelNo", contact.OfficeLevelNo);
            _sPara.Add("@HomeStreet", contact.HomeStreet);
            _sPara.Add("@OfficeStreet", contact.OfficeStreet);
            _sPara.Add("@HomeFax", contact.HomeFax);
            _sPara.Add("@OfficeFax", contact.OfficeFax);
            _sPara.Add("@HomePhone", contact.HomePhone == string.Empty ? string.Empty : contact.HomePhone);
            _sPara.Add("@OfficePhone", contact.OfficePhone == string.Empty ? string.Empty : contact.OfficePhone);
            _sPara.Add("@HomeCity", contact.HomeCity);
            _sPara.Add("@HomeCountry", contact.HomeCountry);
            _sPara.Add("@OfficeCity", contact.OfficeCity);
            _sPara.Add("@OfficeCountry", contact.OfficeCountry);
            _sPara.Add("@Status", contact.Status);
            _sPara.Add("@OtherContact1", contact.OtherContact1);
            _sPara.Add("@OtherContact2", contact.OtherContact2);
            _sPara.Add("@RefCEUserId", contact.RefCEUserId);
            _sPara.Add("@Info1", contact.Info1);
            _sPara.Add("@Info2", contact.Info2);
            _sPara.Add("@Info3", contact.Info3);
            _sPara.Add("@Info4", contact.Info4);
            _sPara.Add("@Info5", contact.Info5);
            _sPara.Add("@Info6", contact.Info6);
            _sPara.Add("@Info7", contact.Info7);
            _sPara.Add("@Info8", contact.Info8);
            _sPara.Add("@Info9", contact.Info9);
            _sPara.Add("@Info10", contact.Info10);
            _sPara.Add("@Info11", contact.Info11);
            _sPara.Add("@Info12", contact.Info12);
            _sPara.Add("@Info13", contact.Info13);
            _sPara.Add("@Info14", contact.Info14);
            _sPara.Add("@Info15", contact.Info15);
            _sPara.Add("@Info16", contact.Info16);
            _sPara.Add("@Info17", contact.Info17);
            _sPara.Add("@Info18", contact.Info18);
            _sPara.Add("@Info19", contact.Info19);
            _sPara.Add("@Info20", contact.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Contact GetContact(Int64 RefCEUserID)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocUserContactSelectSingleItem";
            Contact _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserID);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new Contact();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        private Contact FillDataRecord(DataTable myDataTable)
        {
            Contact myContact = new Contact();
            myContact.ContactId = Convert.ToInt64(myDataTable.Rows[0]["ContactId"].ToString());
            myContact.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());

            string _email = myDataTable.Rows[0]["Email"].ToString();
            if (_email != string.Empty)
            {
                myContact.Email = _email;
            }

            string _mobile = myDataTable.Rows[0]["Mobile"].ToString();
            if (_mobile != string.Empty)
            {
                myContact.Mobile = _mobile;
            }

            string _homephone = myDataTable.Rows[0]["HomePhone"].ToString();
            if (_homephone != string.Empty)
            {
                myContact.HomePhone = _homephone;
            }

            myContact.HomeFax = myDataTable.Rows[0]["HomeFax"].ToString();
            myContact.HomeUnitNo = myDataTable.Rows[0]["HomeUnitNo"].ToString();
            myContact.HomeLevelNo = myDataTable.Rows[0]["HomeLevelNo"].ToString();
            myContact.HomeStreet = myDataTable.Rows[0]["HomeStreet"].ToString();
            myContact.HomeCity = myDataTable.Rows[0]["HomeCity"].ToString();
            myContact.HomeCountry = myDataTable.Rows[0]["HomeCountry"].ToString();

            string _homepostcode = myDataTable.Rows[0]["HomePostCode"].ToString();
            if (_homepostcode != string.Empty)
            {
                myContact.HomePostCode = _homepostcode;
            }

            myContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeUnitNo"].ToString();
            myContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeLevelNo"].ToString();

            string _officephone = myDataTable.Rows[0]["OfficePhone"].ToString();
            if (_officephone != string.Empty)
            {
                myContact.OfficePhone = _officephone;
            }

            myContact.OfficeFax = myDataTable.Rows[0]["OfficeFax"].ToString();
            myContact.OfficeStreet = myDataTable.Rows[0]["OfficeStreet"].ToString();
            myContact.OfficeCity = myDataTable.Rows[0]["OfficeCity"].ToString();
            myContact.OfficeCountry = myDataTable.Rows[0]["OfficeCountry"].ToString();

            string _officepostcode = myDataTable.Rows[0]["OfficePostCode"].ToString();
            if (_officepostcode != string.Empty)
            {
                myContact.OfficePostCode = _officepostcode;
            }

            myContact.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myContact.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myContact.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myContact.Info4 = myDataTable.Rows[0]["Info4"].ToString();
            myContact.OtherContact1 = myDataTable.Rows[0]["OtherContact1"].ToString();
            return myContact;
        }
    }
}